package com.example.administrator.questionanswer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.example.administrator.questionanswer.application.MyApplication;
import com.example.administrator.questionanswer.utils.Const;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/4/5 0005.
 */

public class FunctionSelectActivity extends Activity {
    @BindView(R.id.btn_in_answer)
    Button btnInAnswer;
    @BindView(R.id.btn_in_data)
    Button btnInData;
    @BindView(R.id.btn_login_out)
    Button btnLoginOut;

    SharedPreferences sharedPreferences;
    @BindView(R.id.btn_in_mine)
    Button btnInMine;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actiivty_fuction_select);
        ButterKnife.bind(this);
        MyApplication.getActivies().add(this);
        sharedPreferences = getSharedPreferences(Const.USER_INFO, MODE_PRIVATE);

    }

    @OnClick({R.id.btn_in_answer, R.id.btn_in_data, R.id.btn_login_out, R.id.btn_in_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_in_answer:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.btn_in_mine:
                startActivity(new Intent(this, DataShowActivity.class));
                break;
            case R.id.btn_in_data:
                startActivity(new Intent(this, DataCompileActivity.class));
                break;
            case R.id.btn_login_out:
                showDialog();
                break;
        }
    }


    AlertDialog alertDialog;

    public void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        alertDialog = builder.create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        WindowManager windowManager = window.getWindowManager();
        Display defaultDisplay = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = (int) (defaultDisplay.getWidth() * 0.8);
        window.setAttributes(attributes);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_register, null);
        alertDialog.setContentView(view);
        window.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_white));
        TextView txtsure = (TextView) view.findViewById(R.id.txt_accept);
        TextView txtcancle = (TextView) view.findViewById(R.id.txt_refuse);
        TextView messgae = (TextView) view.findViewById(R.id.txt_user_name);
        TextView title = (TextView) view.findViewById(R.id.txt_friends_type);
        title.setText("退出");
        messgae.setText("确认退出？");
        txtsure.setOnClickListener(onClickListener);
        txtcancle.setOnClickListener(onClickListener);
        alertDialog.setCanceledOnTouchOutside(false);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            switch (view.getId()) {
                case R.id.txt_accept:
                    alertDialog.dismiss();
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putBoolean(Const.USER_ONLINE, false);
                    edit.commit();
                    for (Activity activity : MyApplication.getActivies()) {
                        activity.finish();
                    }
                    startActivity(new Intent(FunctionSelectActivity.this, LoginActivity.class));
                    break;
                case R.id.txt_refuse:
                    alertDialog.dismiss();
                    break;
            }
        }
    };

}
